export const APP_KEY = 'N0icsx5KlHAVZN8nEYY0Xggt+rdZHPHhaDJf1PYYaXM='
export const SIGN_KEY = 'sMqXK6An3xR4ZmfHBXQYPztRt4MM8aasoaM2FcdB9LjkUk79oeFi8zfJ6d6U'

import cryptoJs from 'crypto-js'
export const parse = data => {
  // 如果禁用加密,直接返回原始数据
  // if (!isEnableEncryption) {
  //   return JSON.stringify(data)
  // }
  let resetFromData = {} //处理后接口参数集合
  let sign_key = SIGN_KEY
  let param = {}
  let keys = []
  resetFromData = JSON.parse(JSON.stringify(data))
  resetFromData.time = Math.floor(new Date().getTime() / 1000)
  Object.keys(resetFromData).forEach(function (key) {
    param[key] = resetFromData[key]
  })
  for (let key in param) {
    // 注意这里，要剔除掉 sign 参数本身
    if (key !== 'sign' && key !== 'image') {
      keys.push(key)
    }
  }
  keys.sort()
  // 转成键值对
  let paramPair = []
  for (let i = 0, len = keys.length; i < len; i++) {
    let k = keys[i]
    paramPair.push(k + '=' + encodeURIComponent(param[k])) // urlencode 编码
  }
  let stringSignTemp = paramPair.join('&')
  let sign = cryptoJs
    .MD5(stringSignTemp + '&' + sign_key)
    .toString()
    .toUpperCase()
  resetFromData.sign = sign
  var requestArr = {}
  Object.keys(resetFromData).forEach(function (key) {
    requestArr[key] = resetFromData[key]
  })
  var res_json = JSON.stringify(requestArr)
  var app_key = APP_KEY
  var key = cryptoJs.enc.Base64.parse(app_key)
  var iv = cryptoJs.lib.WordArray.random(16)
  var encrypted = cryptoJs.AES.encrypt(res_json, key, {
    iv: iv,
    mode: cryptoJs.mode.CBC,
    adding: cryptoJs.pad.ZeroPadding
  }).ciphertext.toString(cryptoJs.enc.Base64)
  var iv = cryptoJs.enc.Base64.stringify(iv)
  var mac = cryptoJs.HmacSHA256(iv + encrypted, key)

  var after = {
    iv: iv,
    value: encrypted,
    mac: cryptoJs.enc.Hex.stringify(mac),
    tag: ''
  }
  var after1 = cryptoJs.enc.Utf8.parse(JSON.stringify(after))
  var after2 = cryptoJs.enc.Base64.stringify(after1)
  return after2
}
export const decode = response => {
  if (!isEnableEncryption) {
    return response
  }

  var data1 = cryptoJs.enc.Base64.parse(response.data).toString(cryptoJs.enc.Utf8)
  data1 = JSON.parse(data1)
  var app_key = APP_KEY
  var key1 = cryptoJs.enc.Base64.parse(app_key)
  var iv1 = cryptoJs.enc.Base64.parse(data1.iv)
  var decrypted = cryptoJs.AES.decrypt(data1.value, key1, {
    iv: iv1,
    mode: cryptoJs.mode.CBC,
    adding: cryptoJs.pad.ZeroPadding
  }).toString(cryptoJs.enc.Utf8)
  const a = JSON.parse(decrypted)
  response.data = a
  return response
}
// dlBsz2PLG8tqFt5k3S1A
export const guid = () => {
  return 'yxxxxxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
    var r = (Math.random() * 16) | 0,
      v = c == 'x' ? r : (r & 0x3) | 0x8
    return v.toString(16)
  })
}

export const aesEncrypt = (dataStr, key) => {
  var keyHex = cryptoJs.enc.Utf8.parse(key)
  dataStr = cryptoJs.enc.Utf8.parse(dataStr)
  let encrypted = cryptoJs.AES.encrypt(dataStr, keyHex, {
    // iv,
    mode: cryptoJs.mode.ECB,
    padding: cryptoJs.pad.Pkcs7
  })
  return cryptoJs.enc.Base64.stringify(encrypted.ciphertext)
}
export const aesDecrypt = (encrypted, key) => {
  var keyHex = cryptoJs.enc.Utf8.parse(key)
  let decrypted = cryptoJs.AES.decrypt(encrypted, keyHex, {
    // iv,
    mode: cryptoJs.mode.ECB,
    padding: cryptoJs.pad.Pkcs7
  })
  return decrypted.toString(cryptoJs.enc.Utf8)
}